﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using SpringBeansEditor.Providers;
using System.Collections.ObjectModel;
using AssemblyLoader;

namespace SpringBeansEditor.Editors
{
    /// <summary>
    /// BeanEditor allows users to update a XmlElement of type object.
    /// </summary>
    public partial class BeanEditor : UserControl, IEditor
    {
        XmlElement _xml_element_to_update;
        public delegate void OnBeanChangedDelegate();
        public OnBeanChangedDelegate OnBeanChanged { get; set; }

        string _prev_id, _prev_type;

        public void OnAssemblyUpdated(TypesProvider reflection_provider)
        {
            TypeCmb.Items.Clear();
            var types = reflection_provider.GetTypesAvailable();
            foreach (var type in types)
            {
                TypeCmb.Items.Add(type);
            }
        }

        public void OnXmlUpdated(XmlDocument beans_xml_conf)
        {
            //do nothing
        }

        public void ShowXMLElement(XmlElement object_element)
        {
            IDTxt.Text = object_element.Attributes["id"].Value;
            string[] fulltype = object_element.Attributes["type"].Value.Split(',');
            if (fulltype.Length > 1)
            {
                TypeTxt.Text = fulltype[0];
                AssemblyTxt.Text = fulltype[1].Trim();
            }
            _xml_element_to_update = object_element;

            _prev_id = IDTxt.Text;
            _prev_type = TypeTxt.Text;
        }
        public void Show()
        {
            Visibility = System.Windows.Visibility.Visible;
        }
        public void Hide()
        {
            Visibility = System.Windows.Visibility.Collapsed;
        }

        public BeanEditor()
        {
            InitializeComponent();
        }

        private void SaveBtn_Click(object sender, RoutedEventArgs e)
        {
            //Update global register of types and objects
            TypesProvider reflection_provider = TypesProvider.GetInstance();
            reflection_provider.RemoveTypeUsed(_prev_id, TypeTxt.Text);
            reflection_provider.AddTypeUsed(IDTxt.Text, TypeTxt.Text);

            _xml_element_to_update.Attributes["id"].Value = IDTxt.Text;
            _xml_element_to_update.Attributes["type"].Value = TypeTxt.Text + ", " + AssemblyTxt.Text;
            OnBeanChanged();
        }

        private void TypeTxt_TextChanged(object sender, TextChangedEventArgs e)
        {
            TypeCmb.Items.Filter = new Predicate<object>(NameFilter);
        }

        public bool NameFilter(object item)
        {
            TypeInfo type = item as TypeInfo;
            return (type.Name.ToLower().Contains(TypeTxt.Text.ToLower()));
        }

        private void TypeCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TypeInfo type = ((ListBox)sender).SelectedItem as TypeInfo;
            if (type != null)
            {
                TypeTxt.Text = type.Name;
                AssemblyTxt.Text = type.AssemblyName;
            }

        }

    }
}
